<?php

/**
 * 站内短信管理
 * Enter description here ...
 * @author Admin
 *
 */
class LetterAction extends CommonAction{
	
	
	/**
	 * 站内短信列表
	 * Enter description here ...
	 */
	public function index() {
		$letter = D('Letter');;		
		
		$this->page($letter,$letterdata,$page);
		
		$this->assign("page",$page);
		$this->assign("letterdata",$letterdata);
		
		$this->display();
	}
	
	
	/**
	 * 发送站内信
	 * Enter description here ...
	 */
	public function send() {
		$this->display();
	}
	
	
	/**
	 * 保存站内短信
	 * @see CommonAction::insert()
	 */
	public function insert(){
		$data = Init_GP(array('receptionname','state','message'));
		
		$letter = D('Letter');
		$config = C('SYSCONFIG');
		if (empty($data['message'])){
			$this->error('内容不能为空');
		}
		
		$user = D('User');
		if ($data['state'] == 1){  //广播站内短信
			
			$userdata = $user->field('name')->findAll();  //查询所有系统注册用户
			
			foreach ($userdata as $value){
				$letterdata[] = array(
					'sendname'=>$config['webname'],
					'receptionname'=>$value['name'],
					'message'=>$data['message'],
					'type'=>0,
					'addtime'=>time(),
				);
			}
			$info = $letter->addALL($letterdata);		
			
		}else {
			if (empty($data['receptionname'])){
				$this->error('非全局广播请输入用户昵称');
			}
			
			
			$where['name'] = $data['receptionname'] ;
			$usr = $user->where($where)->findAll() ;
			if(empty($usr)){
				$this->error('该用户昵称的用户在系统中不存在');
			}
			
			
			$letterdata = array(
				'sendname'=>$config['webname'],
				'receptionname'=>$data['receptionname'],
				'message'=>$data['message'],
				'type'=>0,
				'addtime'=>time(),
			);
			$info = $letter->add($letterdata);
			
		}
		
		if ($info) {
			$this->assign('jumpUrl','__URL__/index'); 
			$this->success('发送成功');
			
		}else{
			$this->error('发送失败');
		}
		
	}

	
	public function add(){
		$this->error('操作错误');
	}

	public function edit(){
		$this->error('操作错误');
	}
	
	public function update(){
		$this->error('操作错误');
	}
}


?>